package com.atguigu.day10;

import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.table.api.SqlDialect;
import org.apache.flink.table.api.bridge.java.StreamTableEnvironment;
import org.apache.flink.table.catalog.hive.HiveCatalog;

public class Flink07_HiveCatalog {
    public static void main(String[] args) {
        //1.获取流的执行环境
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        env.setParallelism(1);

        //2.获取表的环境
        StreamTableEnvironment tableEnv = StreamTableEnvironment.create(env);

        //TODO 3.创建HiveCataLog
        String name = "hiveCataLog";
        String databases = "default";
        String confDir = "C:\\conf";

        HiveCatalog hiveCatalog = new HiveCatalog(name, databases, confDir);

        //4.注册CataLog
        tableEnv.registerCatalog(name, hiveCatalog);

        //使用CataLog
        tableEnv.useCatalog(name);
        tableEnv.useDatabase(databases);

        //sql方言 1.13.0版本有bug
        tableEnv.getConfig().setSqlDialect(SqlDialect.HIVE);

        //5.写sql查询hive中数据
        tableEnv.executeSql("select * from student").print();
    }
}
